;(function () {
  Array.prototype.findArr = function() {
    var res = [this[0]];
    for (var i = 1; i < this.length; i++) {
      var repeat = false;
      for (var j = 0; j < res.length; j++) {
        if (this[i].projectId == res[j].projectId) {
          repeat = true;
        }
      }
      if (!repeat) {
        res.push(this[i]);
      }
    }
    return res;
  };

  var priceList = new Vue({
    el:'#priceList',
    data:{
      quanxian: window.localStorage.getItem('permissions'),
      curPage:1,                  // 当前页
      priceData:{},                // 用户数据
      currentPage:1,
      totalPage: 0,
      totalCount: 0,
      pageNums: [{name: '10', code: '10'}, {name: '15', code: '15'}, {name: '20', code: '20'}],
      limit: ['10'],
      createPop:false,             // 新增弹出层
      editPop:false,               // 编辑弹出层
      addUser:{
        account:'',
        passwd:'',
        name:'',
        phone:'',
        email:''
      },    // 新增用户信息
      editUser:{},                // 编辑用户信息
      psd:'',                      // 密码
      project:[],
      projectId: [],
      building:[],
      buildingId: [],
      groupId:null,
      groupOneData:[],
      groupTwoData:[],
      groupThreeData:[],
      selectedGroupData:[],
      isExtenseFlag:false
    },
    filters: {
      formatDate: function (time) {
        if (time === undefined) return '--'
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
      },
      formatUndefined:function (item) {
        if(item === undefined) return '--'
        return item
      },
      company:function (item) {
        if(!item)return '--'
        var itemArr = item.split(',')
        if(itemArr.length === 1){
          return itemArr[0]
        }
        if(itemArr.length >= 1){
          return itemArr[0] + '...'
        }
      },
      organization:function (item) {
        var tmpArr = [];
        if(item.length === 0)return '--'
        if(item.length !== 0){
          for(i = 0; i < item.length; i++){
            tmpArr.push(item[i].join(' - '));
          }
        }
        if(tmpArr.length == 1) return tmpArr[0]
        if(tmpArr.length >= 1) return tmpArr[0] + '...'
      }
    },
    methods:{
      // 获取项目数据
      getProjectData:function(){
        var self = this;
        hlhttp.get({
          url:'user/projects',
          success:function(data){
            data = data.findArr();
						self.project = hlArrayUtil.replaceData(data, [{key: 'projectName', rKey: 'name'}, {key: 'projectId', rKey: 'code'}]);
            self.projectId = self.project[0]['code']

            let selectedPro = []
            selectedPro.push(self.project[0])
            self.getGroupPrimaryData(selectedPro)
          }
        })
      },
      // 获取一级组合数据
      getGroupPrimaryData(data){
        const self = this
        self.projectId = [data[0].code]
        self.buildingId = []
        self.selectedGroupData = []
        this.getGroupData().then(data => {
          self.groupOneData = hlArrayUtil.replaceData( data, [{key: 'id', rKey: 'code'}] );
        })
        self.getBuildingData()
      },
      // 选取一级组合数据
      selectOneGroup(res){
        const self = this
        this.groupId = res['cur']['code']
        this.getGroupData().then(data => {
          self.groupTwoData = hlArrayUtil.replaceData( data, [{key: 'id', rKey: 'code'}] )
          self.getBuildingData()
        })
      },
      // 选取二级组合数据
      selectTwoGroup(res){
        const self = this
        this.groupId = res['cur']['code']
        this.getGroupData().then(data => {
          self.groupThreeData = hlArrayUtil.replaceData( data, [{key: 'id', rKey: 'code'}] )
          self.getBuildingData()
        })
      },
      // 选取三级组合数据
      selectThreeGroup(res){
        this.groupId = res['cur']['code']
      },
      getGroupData(){
        const self = this

        return new Promise(resolve => {
          hlhttp.get({
            url:'project/grouping_info/_subclass',
            data:{'project_id':self.projectId[0] , 'grouping_id':self.groupId || '#' },
            success:function(data){
              resolve(data)
            }
          })
        })
      },
      newAddprice: function () {
        hlUrlUtil.goTo('priceEdit', {ddtab:true})
      },
      editUserMes: function (pname,bname,bid) {
        var data = {
          pname: encodeURI(encodeURI(pname)),
          bname: encodeURI(encodeURI(bname)),
          pid: this.projectId,
          bid: bid,
					ddtab:true
        }
        hlUrlUtil.goTo('priceEdit', data)
      },
      selectedProject:function(arr){
        self.getBuildingData(arr[0].code)
      },
      // 获取项目楼宇列表数据
      getBuildingData:function(){
        var self = this;
        hlhttp.get({
          url:'project/grouping/_building',
          data:{ project_id: self.projectId.length ? self.projectId[0] : '#'  , grouping_id:self.groupId || '#'},
          success:function(data){
            data = data.filter(el => el.configState == 'Y')
            self.building = hlArrayUtil.replaceData(data, [{key: 'buildingId', rKey: 'code'}]);
          }
        })
      },
      selectedBuilding:function(arr){
        var self = this;
        self.buildingId = [arr[0].code];
      },
      // 获取查询的用户数据
      getSearchUserData:function () {
        var self = this;

        hlhttp.get({
          url: 'buildings/price_policys/_search',
          data: {
            page: self.currentPage,
            page_size:  self.limit[0],
            project_id: self.projectId.length ? self.projectId[0] : '#',
            group_id:self.groupId || '#',
            building_id: self.buildingId[0] ? self.buildingId[0] : '#'
          },
          success: function (data) {
            self.totalPage = data.totalPage
            self.totalCount = data.totalCount
            self.priceData = data
          }
        })
      },
      // 查询
      search:function () {
        this.curPage = 1
        this.limit = ['10']
        var valid = this.validate()
        if(!valid){ return false; }

        this.getSearchUserData()
      },
			//翻页数量
			turnPage (item) {
				this.page = 1;
				this.limit = [item[0].code];
				this.getSearchUserData()
			},
      // 重置
      reset:function () {
        var self = this;

        self.projectId = self.project[0]['code'].split()
        self.selectedGroupData = []
        self.groupOneData = []
        self.groupTwoData = []
        self.groupThreeData = []
        this.groupId = null
        self.building = [];
        self.buildingId = [];
        self.currentPage = 1

				this.getSearchUserData()
      },
      // 更换分页条数加载数据
      updateLimit:function (num) {
        this.limit = num;
        var beginTime = $('input[name=beginTime]').val();
        var endTime = $('input[name=endTime]').val();
        var keyWord = $('.js-val-info').val();
        this.getSearchUserData()
      },
      // 停启用状态  Y启用、N禁用
      updateState:function (val,id,i) {
        var opt,self = this;
        if(val == 'Y')opt = 'N'
        if(val == 'N')opt = 'Y'

        hlhttp.put({
          url:'price_policys/' + id + '/valid',
          data:{opt:opt},
          success:function (data) {
            self.$set(self.$data.priceData.policys[i],'valid',opt)
          }
        })
      },
      // 查询验证
      validate:function(){
        if(this.projectId === null){
          hl.toast('请选择项目')
          return false;
        }
        return true;
      }
    },
    mounted:function () {
      hlSelectUi()
      this.getProjectData()
    }
  })
}())
